package com.leinardi.android.speeddial;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

import timber.log.Timber;

public class AdvSpeedDialView extends AbstractSpeedDialView{

    private int hotCount = 1;

    DragDoubleTouchViewAdapter clickAdapter;

    public AdvSpeedDialView(Context context) {
        super(context);
    }

    public AdvSpeedDialView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AdvSpeedDialView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void inflate(int menuRes) {
        clearActionItems();
        SpeedDialActionItem actionItem;
        MenuXmlParser parser=new MenuXmlParser(getContext());
        parser.inflate(menuRes);
        MenuState rootMenu=parser.rootMenu;
        List<MenuState> menuStateList=rootMenu.listAllMenuItem();
        MenuState menuItem;
        int defaultColor;
        int menuSize = menuStateList.size();
        if( menuSize == 0) return;
        Timber.d("inflate menu count:%d",menuSize);
        for (int i = 0; i < menuSize; i++) {
            menuItem = menuStateList.get(i);
            Timber.d("inflate menu id:%d \t title:%s,\t iconResId:%d",menuItem.getItemId(),menuItem.getTitle(),menuItem.getIconResId());
            if(isSpeedDialFabBackgroundColorRandom)
                defaultColor = UiUtils.randomColor();
            else
                defaultColor = speedDialDefaultBackgroudColor;
            if( i >= hotCount )
                actionItem = new SpeedDialActionItem.Builder(menuItem.getItemId(), menuItem.getIconResId())
                        .setLabel(menuItem.getTitle() != null ? menuItem.getTitle(): null)
                        .setFabBackgroundColor(defaultColor)
                        .create();
            else
                actionItem = new SpeedDialActionItem.Builder(menuItem.getItemId(), menuItem.getIconResId())
                        .setFabBackgroundColor(defaultColor)
                        .setHot(true).create();
            if( i == hotCount ) {
                addActionItem(new SpeedDialActionItem.Builder(R.id.sd_speed_dial_fab,android.R.drawable.ic_menu_add).setFabBackgroundColor(speedDialDefaultBackgroudColor).setHot(true).create());
            }
            addActionItem(actionItem);
        }
    }

    @Override
    protected void initMainFabWithClickListener(FloatingActionButton fab) {
        clickAdapter=new DragDoubleTouchViewAdapter(fab,this,UiUtils.getScreenWidth(getContext()),UiUtils.getScreenHeight(getContext()),UiUtils.getStatusHeight(getContext()));
    }

    @Override
    public void setOnActionSelectedListener(@Nullable OnActionSelectedListener listener) {
        super.setOnActionSelectedListener(listener);
    }

    @Override
    public void show(@Nullable FloatingActionButton.OnVisibilityChangedListener listener) {
        super.show(listener);
        clickAdapter.halfHovering(true);
        clickAdapter.delayHalfHovering();
    }

    @Override
    public void close(boolean animate) {
        super.close(animate);
        clickAdapter.delayHalfHovering();
    }

    public int getHotCount() {
        return hotCount;
    }

    public void setHotCount(int hot_count) {
        this.hotCount = hot_count;
    }

    public void setDoubleClickCallback(DragDoubleTouchViewAdapter.DoubleClickCallback mCallback) {
        clickAdapter.setDoubleClickCallback(mCallback);
    }

    public void setLongClickCallback(DragDoubleTouchViewAdapter.LongClickCallback mLongClickCallback) {
        clickAdapter.setLongClickCallback(mLongClickCallback);
    }

    public void setClickCallback(DragDoubleTouchViewAdapter.ClickCallback clickCallback) {
        clickAdapter.setClickCallback(clickCallback);
    }

    public void setHalfHoveringStateChangeCallback(DragDoubleTouchViewAdapter.HalfHoveringStateChangeCallback callback){
        clickAdapter.setHalfHoveringStateChangeCallback(callback);
    }

    public void setImageResource(@DrawableRes int id) {
        mMainFab.setImageResource(id);
    }
}
